package com.easypay.reactive8583.netty.codec;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.util.CharsetUtil;
import lombok.Data;

import java.nio.ByteOrder;

/**
 * @author pandans
 * @date 2021/03/04 16:33
 * @Description:
 */
@Data
public class StringLengthFieldBasedFrameDecoder extends LengthFieldBasedFrameDecoder {
    private int maxFrameLength;
    private int lengthFieldOffset;
    private int lengthFieldLength;
    private int lengthAdjustment;
    private int initialBytesToStrip;

    public StringLengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip);
    }

    @Override
    public long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
        ByteBuf b = buf;
        //TODO order 方法过时----
        b = b.order(order);
        byte[] lengthBytes = new byte[length];
        b.getBytes(offset, lengthBytes);
        String s = new String(lengthBytes, CharsetUtil.US_ASCII);
        return Long.valueOf(s);
    }

}
